

***********************
** Study 2: Figure S2.9
***********************


** 2017
*******

* Macro
global covars_binaries = "gender1 gender2 gender3 agecat1 agecat2 agecat3 agecat4 agecat5 agecat6 edu1 edu2 edu3 edu4 edu5 edu6 pol_interest1 pol_interest2 pol_interest3 pol_interest4 lr0 lr1 lr2 lr3 lr4 lr5 lr6 lr7 lr8 lr9 lr10 vote2015_1 vote2015_2 vote2015_3 vote2015_4 vote2015_5 vote2015_6 vote2015_7 vote2015_8 vote2015_9 vote2015_10 refvote2016_1 refvote2016_2 refvote2016_3 region1 region2 region3"

* Prepare variables to save pre/post means and variances
use "LondonBridgeAttack2017.dta", replace
gen covariate = ""
local a = 0
foreach var of varlist $covars_binaries {
local a = `a' + 1
replace covariate = "`var'" in `a'
}
gen mean_pre1 = .
gen mean_post1 = .
gen mean_pre2 = .
gen mean_post2 = .
gen mean_pre3 = .
gen mean_post3 = .

gen sd_pre1 = .
gen sd_post1 = .
gen sd_pre2 = .
gen sd_post2 = .
gen sd_pre3 = .
gen sd_post3 = .


* Binary covariate labels
gen X = _n
replace X = . if covariate == ""
label define X 1 "Gender = Female" 2 "Gender = Male" 3 "Gender = Other" ///
	4 "Age < 25" 5 "Age = 25-34" 6 "Age = 35-44" 7 "Age = 45-54" 8 "Age = 55-64" 9 "Age = 65+" ///
	10 "Educ. = <Secondary" 11 "Educ. = Secondary" 12 "Educ. = Further" 13 "Educ. = Techn./Vocat." 14 "Educ. = University" 15 "Educ. = Postgraduate" ///
	16 "Pol. int. = Not at all" 17 "Pol. int, = Not very" 18 "Pol. int. = Fairly" 19 "Pol. int. = Very" ///
	20 "LR = 0 (left)" 21 "LR = 1" 22 "LR = 2" 23 "LR = 3" 24 "LR = 4" 25 "LR = 5" 26 "LR = 6" 27 "LR = 7" 28 "LR = 8" 29 "LR = 9" 30 "LR = 10 (right)" ///
	31 "GE 2015 = Ineligible" 32 "GE 2015 = Did not vote" 33 "GE 2015 = Con" 34 "GE 2015 = Lab" 35 "GE 2015 = LibDem" 36 "GE 2015 = Green" 37 "GE 2015 = UKIP" 38 "GE 2015 = Other" 39 "GE 2015 = SNP" 40 "GE 2015 = Plaid" ///
	41 "Ref 2016 = Did not vote" 42 "Ref 2016 = Remain" 43 "Ref 2016 = Leave" 44 "Region = England" 45 "Region = Scotland" 46 "Region = Wales", replace
label value X X
order X, before(covariate)

* Generate unadjusted pre/post means and variances
foreach var of varlist $covars_binaries {
sum `var' if statadate == td(02jun2017)
replace mean_pre1 = `r(mean)' if covariate == "`var'"
replace sd_pre1 = `r(Var)' if covariate == "`var'"
sum `var' if statadate == td(04jun2017)
replace mean_post1 = `r(mean)' if covariate == "`var'"
replace sd_post1 = `r(Var)' if covariate == "`var'"
sum `var' if inrange(statadate,td(01jun2017),td(02jun2017))
replace mean_pre2 = `r(mean)' if covariate == "`var'"
replace sd_pre2 = `r(Var)' if covariate == "`var'"
sum `var' if inrange(statadate,td(04jun2017),td(05jun2017))
replace mean_post2 = `r(mean)' if covariate == "`var'"
replace sd_post2 = `r(Var)' if covariate == "`var'"
sum `var' if inrange(statadate,td(31may2017),td(02jun2017))
replace mean_pre3 = `r(mean)' if covariate == "`var'"
replace sd_pre3 = `r(Var)' if covariate == "`var'"
sum `var' if inrange(statadate,td(04jun2017),td(06jun2017))
replace mean_post3 = `r(mean)' if covariate == "`var'"
replace sd_post3 = `r(Var)' if covariate == "`var'"
}

* Standardized difference of means
gen std_diff_means1 = (mean_post1 - mean_pre1)/sqrt((sd_post1 + sd_pre1)/2)
gen std_diff_means2 = (mean_post2 - mean_pre2)/sqrt((sd_post2 + sd_pre2)/2)
gen std_diff_means3 = (mean_post3 - mean_pre3)/sqrt((sd_post3 + sd_pre3)/2)

* Save results
keep X - std_diff_means3
sort X
keep in 1/50
save "Balance2017.dta", replace






** 2019
*******

* Macro
global covars_binaries = "gender1 gender2 gender3 agecat1 agecat2 agecat3 agecat4 agecat5 agecat6 edu1 edu2 edu3 edu4 edu5 edu6 pol_interest1 pol_interest2 pol_interest3 lr0 lr1 lr2 lr3 lr4 lr5 lr6 lr7 lr8 lr9 lr10 vote2017_1 vote2017_2 vote2017_3 vote2017_4 vote2017_5 vote2017_6 vote2017_7 vote2017_8 vote2017_9 vote2017_10 vote2017_11 refvote2016_1 refvote2016_2 refvote2016_3 refvote2016_4 region1 region2 region3"


* Prepare variables to save pre/post means and variances
use "LondonBridgeAttack2019.dta", replace
gen covariate = ""
local a = 0
foreach var of varlist $covars_binaries {
local a = `a' + 1
replace covariate = "`var'" in `a'
}
gen mean_pre1 = .
gen mean_post1 = .
gen mean_pre2 = .
gen mean_post2 = .
gen mean_pre3 = .
gen mean_post3 = .

gen sd_pre1 = .
gen sd_post1 = .
gen sd_pre2 = .
gen sd_post2 = .
gen sd_pre3 = .
gen sd_post3 = .

* Binary covariate labels
gen X = _n
replace X = . if covariate == ""
label define X 1 "Gender = Female" 2 "Gender = Male" 3 "Gender = Other" ///
	4 "Age < 25" 5 "Age = 25-34" 6 "Age = 35-44" 7 "Age = 45-54" 8 "Age = 55-64" 9 "Age = 65+" ///
	10 "Educ. = <Secondary" 11 "Educ. = Secondary" 12 "Educ. = Techn./Vocat." 13 "Educ. = Further" 14 "Educ. = University" 15 "Educ. = Postgraduate" ///
	16 "Pol. int. = Not much" 17 "Pol. int, = Some" 18 "Pol. int. = A good deal" ///
	19 "LR = 0 (left)" 20 "LR = 1" 21 "LR = 2" 22 "LR = 3" 23 "LR = 4" 24 "LR = 5" 25 "LR = 6" 26 "LR = 7" 27 "LR = 8" 28 "LR = 9" 29 "LR = 10 (right)" ///
	30 "GE 2017 = Did not vote" 31 "GE 2017 = Spoiled" 32 "GE 2017 = Ineligible" 33 "GE 2017 = Con" 34 "GE 2017 = Lab" 35 "GE 2017 = LibDem" 36 "GE 2017 = Green" 37 "GE 2017 = UKIP" 38 "GE 2017 = Other" 39 "GE 2017 = SNP" 40 "GE 2017 = Plaid" ///
	41 "Ref 2016 = Did not vote" 42 "Ref 2016 = Ineligible" 43 "Ref 2016 = Remain" 44 "Ref 2016 = Leave" 45 "Region = England" 46 "Region = Scotland" 47 "Region = Wales", replace
label value X X
order X, before(covariate)

* Generate unadjusted pre/post means and variances
foreach var of varlist $covars_binaries {
sum `var' if statadate == td(28nov2019)
replace mean_pre1 = `r(mean)' if covariate == "`var'"
replace sd_pre1 = `r(Var)' if covariate == "`var'"
sum `var' if statadate == td(30nov2019)
replace mean_post1 = `r(mean)' if covariate == "`var'"
replace sd_post1 = `r(Var)' if covariate == "`var'"
sum `var' if inrange(statadate,td(27nov2019),td(28nov2019))
replace mean_pre2 = `r(mean)' if covariate == "`var'"
replace sd_pre2 = `r(Var)' if covariate == "`var'"
sum `var' if inrange(statadate,td(30nov2019),td(1dec2019))
replace mean_post2 = `r(mean)' if covariate == "`var'"
replace sd_post2 = `r(Var)' if covariate == "`var'"
sum `var' if inrange(statadate,td(26nov2019),td(28nov2019))
replace mean_pre3 = `r(mean)' if covariate == "`var'"
replace sd_pre3 = `r(Var)' if covariate == "`var'"
sum `var' if inrange(statadate,td(30nov2019),td(2dec2019))
replace mean_post3 = `r(mean)' if covariate == "`var'"
replace sd_post3 = `r(Var)' if covariate == "`var'"
}

* Standardized difference of means
gen std_diff_means1 = (mean_post1 - mean_pre1)/sqrt((sd_post1 + sd_pre1)/2)
gen std_diff_means2 = (mean_post2 - mean_pre2)/sqrt((sd_post2 + sd_pre2)/2)
gen std_diff_means3 = (mean_post3 - mean_pre3)/sqrt((sd_post3 + sd_pre3)/2)

* Save results
keep X - std_diff_means3
sort X
keep in 1/50
save "Balance2019.dta", replace











** Figure
*********


* 2017
use "Balance2017.dta", replace
twoway ///
	(scatter X std_diff_means1 if X != ., mcolor(midblue) msymbol(oh) yaxis(1 2) xaxis (1 2) msize(medium))  ///
	(scatter X std_diff_means2 if X != ., mcolor(red*.9) msymbol(th) yaxis(1 2) xaxis (1 2) msize(medium))  ///	
	(scatter X std_diff_means3 if X != ., mcolor(gold*.9) msymbol(x) yaxis(1 2) xaxis (1 2) msize(medium))  ///	
	, /// 
	yscale(range(1 46) axis(1 2) reverse) xscale(range(-.5 .5) axis(1 2)) ///
	ylabel(1(1)46, axis(1) gmin gmax labsize(2.25) glwidth(medthin) valuelabel angle(horizontal) ticks) ///
	ylabel(1(1)46, axis(2) gmin gmax nolabels ticks) ///
	xlabel(-0.5 -.25 -.1 0 .1 .25 0.5, axis(1) labsize(2.25) ticks) ///
	xlabel(-0.5 -.25 -.1 0 .1 .25 0.5, axis(2) nolabels ticks) ///
	ytitle("", axis(1)) ytitle("", axis(2)) ///
	xtitle("Standardized difference in means", axis(1) size(3) margin(small)) ///
	xtitle("", axis(2)) ///
	xline(0, lcolor(black) lpattern(solid) lwidth(medthin)) ///
	xline(-.1, lcolor(black) lpattern(shortdash) lwidth(medthin)) ///
	xline(.1, lcolor(black) lpattern(shortdash) lwidth(medthin)) ///
	title(2017, size(medlarge)) ///
	legend(order(1 2 3) label(1 "± 1 day") label(2 "± 2 days") label(3 "± 3 days") cols(1) pos(12) region(lstyle(none) lcolor(white)) size(2.25))  ///
	xsize(8) ysize(12) scale(1.2) aspectratio(2.4) scheme(s2mono) ///
	graphregion(fcolor(white) ilcolor(white) lcolor(white)) bgcolor(white)
graph save "2017.gph", replace	



* 2019
use "Balance2019.dta", replace
twoway ///
	(scatter X std_diff_means1 if X != ., mcolor(midblue) msymbol(oh) yaxis(1 2) xaxis (1 2) msize(medium))  ///
	(scatter X std_diff_means2 if X != ., mcolor(red*.9) msymbol(th) yaxis(1 2) xaxis (1 2) msize(medium))  ///	
	(scatter X std_diff_means3 if X != ., mcolor(gold*.9) msymbol(x) yaxis(1 2) xaxis (1 2) msize(medium))  ///	
	, /// 
	yscale(range(1 47) axis(1 2) reverse) xscale(range(-.5 .5) axis(1 2)) ///
	ylabel(1(1)47, axis(1) gmin gmax labsize(2.25) glwidth(medthin) valuelabel angle(horizontal) ticks) ///
	ylabel(1(1)47, axis(2) gmin gmax nolabels ticks) ///
	xlabel(-0.5 -.25 -.1 0 .1 .25 0.5, axis(1) labsize(2.25) ticks) ///
	xlabel(-0.5 -.25 -.1 0 .1 .25 0.5, axis(2) nolabels ticks) ///
	ytitle("", axis(1)) ytitle("", axis(2)) ///
	xtitle("Standardized difference in means", axis(1) size(3) margin(small)) ///
	xtitle("", axis(2)) ///
	xline(0, lcolor(black) lpattern(solid) lwidth(medthin)) ///
	xline(-.1, lcolor(black) lpattern(shortdash) lwidth(medthin)) ///
	xline(.1, lcolor(black) lpattern(shortdash) lwidth(medthin)) ///
	title(2019, size(medlarge)) ///
	legend(order(1 2 3) label(1 "± 1 day") label(2 "± 2 days") label(3 "± 3 days") cols(1) pos(12) region(lstyle(none) lcolor(white)) size(2.25))  ///
	xsize(8) ysize(12) scale(1.2) aspectratio(2.4) scheme(s2mono) ///
	graphregion(fcolor(white) ilcolor(white) lcolor(white)) bgcolor(white)
graph save "2019.gph", replace	
	
	
* Graph combine
graph combine 2017.gph 2019.gph, iscale(*0.8) xsize(10) ysize(8) graphregion(fcolor(white) lcolor(white) lwidth(vthick)) plotregion(fcolor(white) lcolor(white))




** Tidy Up
**********

erase "Balance2017.dta"
erase "Balance2019.dta"
erase "2017.gph"
erase "2019.gph"



